package com.nondo.dean.auth.provider.impl;

import com.nondo.dean.auth.common.BusinessUtils;
import com.nondo.dean.auth.common.bean.CompanyItem;
import com.nondo.dean.auth.common.bean.CompanyTree;
import com.nondo.dean.auth.domain.Company;
import com.nondo.dean.auth.provider.CompanyManager;
import com.nondo.dean.auth.service.CompanyService;
import com.nondo.dean.auth.service.OfficeService;
import com.nondo.dean.auth.service.PositionService;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * project：dean-cloud
 *
 * @author linfeng @ nondo
 * @date 2018/4/23
 */
@Service
public class CompanyManagerImpl implements CompanyManager {

    @Autowired
    private CompanyService companyService;
    @Autowired
    private OfficeService officeService;
    @Autowired
    private PositionService positionService;

    @Override
    public View<String> requestSave(CompanyItem item) {

        String parentPath = "";
        Company company = BusinessUtils.toCompany(item);
        if (company.getParentId() != null) {
            //父公司不为空，查询出父公司
            Company parentCompany = companyService.findByCompanyId(company.getParentId());
            parentPath = parentCompany.getPath();
        }

        company.setPath(parentPath + "/" + company.getName());
        companyService.saveAndUpdateCompany(company);

        officeService.updateCompanyNameByCompanyId(company.getName(), company.getId());
        positionService.updateCompanyNameByCompanyId(company.getName(), company.getId());

        return View.ofOk();
    }

    @Override
    public View<String> requestDelete(Long companyId) {

        companyService.deleteByCompanyId(companyId);
        return View.ofOk();
    }

    @Override
    public View<String> requestMultiDelete(Long[] companyIds) {

        for (Long id : companyIds) {
            companyService.deleteByCompanyId(id);
        }
        return View.ofOk();
    }

    @Override
    public View<Page<Company>> requestQuery(Query<CompanyItem> item) {

        Query<Company> companyQuery = new Query<>();
        companyQuery.setData(BusinessUtils.toCompany(item.getData()));
        companyQuery.setPage(item.getPage());
        companyQuery.setSize(item.getSize());

        Page<Company> companyPage = companyService.findCompanyPage(companyQuery);

        return View.ofOk(companyPage);
    }

    @Override
    public View<List<CompanyTree>> requestCompanyTreeOfAll() {
        List<CompanyTree> companyTreeList = new ArrayList<>();
        List<Company> companyList = companyService.findAll();

        initRootCompnay(companyTreeList, companyList);

        return View.ofOk("树形公司", companyTreeList);
    }

    @Override
    public View<Company> requestQueryOne(Long id) {

        Company company = companyService.findByCompanyId(id);
        if (company == null) {
            return View.ofError("公司信息没有找到");
        }
        return View.ofOk(company);
    }

    /**
     * 取出顶级公司
     *
     * @param companyTreeList 公司结果集合
     * @param companies       公司集合
     */
    private void initRootCompnay(List<CompanyTree> companyTreeList, List<Company> companies) {
        Iterator<Company> iter = companies.iterator();
        while (iter.hasNext()) {
            Company company = iter.next();
            if (company.getParentId() == null) {
                companyTreeList.add(new CompanyTree(company.getName(), company.getId() + "", company.getId() + ""));
                iter.remove();
            }
        }
        initCompanyTree(companyTreeList, companies);
    }

    /**
     * 转换公司为树形公司
     *
     * @param companyTreeList 公司结果集合
     * @param companies       公司集合
     */
    private void initCompanyTree(List<CompanyTree> companyTreeList, List<Company> companies) {

        List<CompanyTree> childMenuTreeList = new ArrayList<>();
        CompanyTree treeBean;
        for (CompanyTree bean : companyTreeList) {
            Iterator<Company> iter = companies.iterator();
            while (iter.hasNext()) {
                Company company = iter.next();
                if (bean.getKey().equals(company.getParentId() + "")) {
                    treeBean = new CompanyTree(company.getName(), company.getId() + "", company.getId() + "");
                    bean.getChildren().add(treeBean);
                    childMenuTreeList.add(treeBean);
                    iter.remove();
                }
            }
        }
        if (!childMenuTreeList.isEmpty()) {
            initCompanyTree(childMenuTreeList, companies);
        }

    }

}
